package com.mengfou;

/**
 * @author 梦否
 * @date 2023/07/09 15:12
 */
public class SyncDemo {

    interface Callback {
        void onResult(String result);
    }

    public void asyncRequest(final Callback callback) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(9000);
                    callback.onResult("Sleep over time. over 9000ms");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        System.out.println("execute asyncRequest method.");
        int i = 0;
        System.out.println(i++);
    }

    public static void main(String[] args) {
        SyncDemo syncDemo = new SyncDemo();
        Callback callback = new Callback(){
            public void onResult(String result) {
                System.out.println("result : " + result );
            }
        };
        syncDemo.asyncRequest(callback);

        synchronized (callback) {
            try {
                callback.wait(5000);
            } catch (InterruptedException e) {
                callback.onResult("wait for 5000ms, over time!");
                e.printStackTrace();
            }
        }

        System.out.println("execute over! for wait 5000ms");
    }

}
